`
Note that you can also source a bash file using the . command, as in Listing
42.
. ~/.pentest.sh
Listing 2-42
An alternative to the source command
Capturing Terminal Session Activity
Penetration testing often involves having dozens of terminals open
simultaneously, all running many tools that can produce a lot of output. When we
find something of interest, we may need some of that output as evidence for later.
To avoid losing track of an important piece of information, we can make use of
some clever bash.
The script command allows us to capture terminal session activity. One
way to use it is to load a small bash script that uses script to save every session
to a file for later inspection. The script might look like this:
#!/bin/bash
FILENAME="$(date +%m-%d-%y)_${RANDOM}.log"
if [[ ! -d ~/sessions ]]; then
mkdir ~/sessions
fi
# Starting a script session
script -f -a "~/sessions/${FILENAME}"
Having ~/.bashrc load this script, as showed earlier, will result in the creation
of the ~/sessions directory containing each terminal session capture in a separate
file. The recording stops once you enter exit in the terminal or close the entire
terminal window.
Exercise 2: Pinging a Domain
In this exercise, we’ll write a bash script that accepts two arguments: a name
(for example, mysite) and a target domain (for example, nostarch.com). The script
should also be able to do the following:
1. Throw an error if the arguments are missing and exit using the right status
code.
2. Ping the domain and return an indication of whether the ping was
successful or not.
3. Write the results to a CSV file containing the following information:
a.
The name provided to the script
b. The target domain provided to the script
Black Hat Bash (Early Access) © 2023 by Dolev Farhi and Nick Aleks